package com.example.qxfw.nyqx.service.impl;

import com.example.qxfw.nyqx.service.AgriculturalServiceInformationStationService;
import com.example.qxfw.common.entity.ny.CropGrowdateHistory;
import com.example.qxfw.common.entity.ny.StationInfo;
import com.example.qxfw.common.entity.qx.MsgMediumSmallScale;
import com.example.qxfw.common.entity.qx.SmallScaleStation;
import com.example.qxfw.common.mapper.CropGrowdateHistoryMapperXml;
import com.example.qxfw.common.mapper.MsgmediumsmallscaleMapperXml;
import com.example.qxfw.common.mapper.SmallscaleStationMapperXml;
import com.example.qxfw.common.mapper.StationInfoMapperXml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.example.qxfw.common.util.StringUtil.toNull;

/************************************
 *
 * @author : HuangDeYin Jolly
 * @since : JDK 11
 * @Date : 2023/9/11  星期一   8:53
 * @description :
 *
 ***************************************/
@Service
public class AgriculturalServiceInformationStationServiceImpl implements AgriculturalServiceInformationStationService {

    @Autowired
    private StationInfoMapperXml stationInfoMapperXml;

    @Autowired
    private MsgmediumsmallscaleMapperXml msgmediumsmallscaleMapperXml;

    @Autowired
    private SmallscaleStationMapperXml smallscaleStationMapperXml;

    @Autowired
    private CropGrowdateHistoryMapperXml cropGrowdateHistoryMapperXml;

    @Override
    public List<StationInfo> selectAgriculturalServiceInformationStation(final String stationId, final String stationName, final String county) {
        String id = "";
        String name = "";
        String county1 = "";

        if (stationId.equals("全部")) {
            id = null;
        }
        if (stationName.equals("全部")) {
            name = null;
        }
        if (county.equals("全部")) {
            county1 = null;
        }
        return stationInfoMapperXml.queryAgriculturalServiceInformationStation(id, name, county1);
    }

    @Override
    public List<String> selectDistinctStationName() {
        return stationInfoMapperXml.queryStationId();
    }

    @Override
    public List<String> selectDistinctStationId() {
        return stationInfoMapperXml.queryStationName();
    }

    @Override
    public List<String> selectDistinctCounty() {
        return stationInfoMapperXml.queryStationCounty();
    }

    @Override
    public List<MsgMediumSmallScale> getStatisticalChartOfAgriculturalStationElements(final String stationId, final String column, Timestamp startTime, Timestamp endTime) {
        List<MsgMediumSmallScale> msgMediumSmallScaleList = new ArrayList<>();
        List<String> smallScaleStationJoinList = stationInfoMapperXml.selectSmallScaleStationJoin(stationId);
        if (smallScaleStationJoinList != null) {
            List<SmallScaleStation> smallScaleStations = smallscaleStationMapperXml.queryStatisticalChartOfAgriculturalStationElements(smallScaleStationJoinList.get(0));
            smallScaleStations.forEach(smallScaleStation -> {
                List<MsgMediumSmallScale> msgMediumSmallScales = msgmediumsmallscaleMapperXml.selectStatisticalChartOfAgriculturalStationElements(smallScaleStation.getStationid(), column, startTime, endTime);
                msgMediumSmallScales.forEach(msgMediumSmallScale -> {
                    msgMediumSmallScale.setDrybulbtemp(toNull(msgMediumSmallScale.getDrybulbtemp()));
                    msgMediumSmallScale.setRelhumidity(toNull(msgMediumSmallScale.getRelhumidity()));
                    msgMediumSmallScale.setDewtemp(toNull(msgMediumSmallScale.getDewtemp()));
                    msgMediumSmallScale.setWinddirect(toNull(msgMediumSmallScale.getWinddirect()));
                    msgMediumSmallScale.setWindvelocity(toNull(msgMediumSmallScale.getWindvelocity()));
                    msgMediumSmallScale.setStationpress(toNull(msgMediumSmallScale.getStationpress()));
                    msgMediumSmallScale.setEvaporation(toNull(msgMediumSmallScale.getEvaporation()));
                    msgMediumSmallScale.setRain(toNull(msgMediumSmallScale.getRain()));
                    msgMediumSmallScale.setMaxwindv(toNull(msgMediumSmallScale.getMaxwindv()));
                    msgMediumSmallScale.setMaxtemp(toNull(msgMediumSmallScale.getMaxtemp()));
                    msgMediumSmallScale.setMintemp(toNull(msgMediumSmallScale.getMintemp()));
                });
                msgMediumSmallScaleList.addAll(msgMediumSmallScales);
            });
            return msgMediumSmallScaleList.stream().distinct().collect(Collectors.toList());
        } else {
            return null;
        }
    }

    @Override
    public List<CropGrowdateHistory> queryRealSituationQueryOfCropDevelopmentPeriod(final String stationId, final Integer year, final String cropName, final String type) {
        return cropGrowdateHistoryMapperXml.queryRealSituationQueryOfCropDevelopmentPeriod(stationId, year, cropName, type);
    }
}
